console.log("Hello")
let accounts = [];
if (typeof window.ethereum !== 'undefined') {
	console.log('MetaMask is installed!');
}

$(".enableEthereumButton").click(function () {
	confirm("启动链接本地账户中....")

	// ethereum.request({ method: 'eth_requestAccounts' });
	getAccount();
})

async function getAccount() {
	accounts = await ethereum.request({
		method: 'eth_requestAccounts'
	});
	const account = accounts[0];
	nu = account;
	if (accounts[0] != null) {
		alert("链接成功!!!" + "\n" + "当前账户为:" + "\n" + "\n" + accounts[0])
	} else {
		alert('请重新连接.....')
	}
	console.log(accounts)
	// document.getElementById('showAccount').innerHTML ;
	$(".showAccount").html(account)
}
// const Web3 = require('web3');
let web3 = new Web3(Web3.givenProvider || "HTTP://127.0.0.1:7545");
var contractabi = [
	{
		"inputs": [],
		"stateMutability": "nonpayable",
		"type": "constructor"
	},
	{
		"anonymous": false,
		"inputs": [
			{
				"indexed": false,
				"internalType": "address",
				"name": "beneficiary",
				"type": "address"
			},
			{
				"indexed": false,
				"internalType": "uint256",
				"name": "amount",
				"type": "uint256"
			}
		],
		"name": "returnMoney",
		"type": "event"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "id",
				"type": "uint256"
			},
			{
				"internalType": "uint256",
				"name": "_addtime",
				"type": "uint256"
			}
		],
		"name": "AddTime",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "string",
				"name": "_name",
				"type": "string"
			},
			{
				"internalType": "string",
				"name": "_category",
				"type": "string"
			},
			{
				"internalType": "uint256",
				"name": "_startPrice",
				"type": "uint256"
			}
		],
		"name": "addProductToStore",
		"outputs": [
			{
				"internalType": "bool",
				"name": "",
				"type": "bool"
			}
		],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "id",
				"type": "uint256"
			}
		],
		"name": "break_promise",
		"outputs": [
			{
				"internalType": "bool",
				"name": "",
				"type": "bool"
			}
		],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "defaultaddress",
		"outputs": [
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "address",
				"name": "addr",
				"type": "address"
			}
		],
		"name": "getBalance",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "id",
				"type": "uint256"
			}
		],
		"name": "getMoney",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "getNewInformation",
		"outputs": [
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			},
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "_productId",
				"type": "uint256"
			}
		],
		"name": "getProduct",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			},
			{
				"internalType": "string",
				"name": "",
				"type": "string"
			},
			{
				"internalType": "string",
				"name": "",
				"type": "string"
			},
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			},
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			},
			{
				"internalType": "bool",
				"name": "",
				"type": "bool"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "id",
				"type": "uint256"
			}
		],
		"name": "highestBidderInfo",
		"outputs": [
			{
				"internalType": "address",
				"name": "",
				"type": "address"
			},
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "id",
				"type": "uint256"
			}
		],
		"name": "owerbreak_promise",
		"outputs": [
			{
				"internalType": "bool",
				"name": "",
				"type": "bool"
			}
		],
		"stateMutability": "payable",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "productIndex",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "queryBalance",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "id",
				"type": "uint256"
			}
		],
		"name": "setAuctionEnd",
		"outputs": [],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "id",
				"type": "uint256"
			},
			{
				"internalType": "uint256",
				"name": "_timeLimit",
				"type": "uint256"
			}
		],
		"name": "setAuctionStart",
		"outputs": [
			{
				"internalType": "bool",
				"name": "",
				"type": "bool"
			}
		],
		"stateMutability": "nonpayable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "id",
				"type": "uint256"
			}
		],
		"name": "showProductstatus",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			},
			{
				"internalType": "bool",
				"name": "",
				"type": "bool"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "id",
				"type": "uint256"
			}
		],
		"name": "timeRemaining",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "id",
				"type": "uint256"
			}
		],
		"name": "tobid",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "payable",
		"type": "function"
	},
	{
		"inputs": [
			{
				"internalType": "uint256",
				"name": "id",
				"type": "uint256"
			}
		],
		"name": "totalBid",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	},
	{
		"inputs": [],
		"name": "totalBidders",
		"outputs": [
			{
				"internalType": "uint256",
				"name": "",
				"type": "uint256"
			}
		],
		"stateMutability": "view",
		"type": "function"
	}
];

var contract = new web3.eth.Contract(contractabi, "0xE80749c893CD279cE36450C1D9F9C58D851C0f4C");
console.log(contract);

//1.添加商品(已经完成)
$(".sure_add").click(function () {
	let _name = $("#name").val();
	let _category = $("#category").val();
	let _price = $("#price").val();
	console.log("name=>", _name)
	console.log("category=>", _category)
	console.log("price=>", _price)
	const code = 5;
	console.error('error', code);
	if (code != 0) {
		confirm("请先确认已经连接到本地账户")
		document.getElementById('closeBtn').click();
	}
	contract.methods.addProductToStore(_name, _category, _price).send({
		from: accounts[0]
	}).then(
		function (result) {
			console.log("addresult=>", result)
			if (result.status == true) {
				document.getElementById('closeBtn').click();
				confirm("添加的商品信息" + "\n" + "商品名称 :" + _name + "\n" + "商品分类 :" + _category + "\n" + "商品起拍价格 :" + _price + "\n" + "商品当前状态 :等待拍卖")
			}
		}
	)
});

// 2.根据查询弹出显示商品状态
$('.getinfobyid').click(function () {
	let getinputid = $('#getinputid').val();
	console.log('getinputid', getinputid)
	contract.methods.getProduct(getinputid).call().then(
		function (receipt) {
			console.log("receipt=>", receipt)
			$("#searchgoodby_id").html(receipt[0]);
			$("#searchgoodby_name").html(receipt[1]);
			$("#searchgoodby_category").html(receipt[2]);
			$("#searchgoodby_startpri").html(receipt[3]);
			$("#searchgoodby_owner").html(receipt[4]);
			console.log(receipt[4])
			$("#searchgoodby_status").html(receipt[5].toString());
			if (document.getElementById('searchgoodby_status').innerHTML = 'false') {
				$("#searchgoodby_status").html("商品未开始拍卖");
			}
			if (document.getElementById('searchgoodby_owner').innerHTML =='0x0000000000000000000000000000000000000000') {
				alert("商品不存在")
				document.getElementById('closeBtn1').click();
			}
		});
});





//2.设置商品开始（传入商品的id和商品拍卖时间） 
$(".setProductStartbut").click(function () {
	//获取id
	let _setProductStartid = $("#setProductStartid").val();
	//获取时间
	let _setProductStarttimeLimit = $("#setProductStarttimeLimit").val();
	console.log("setProductStartid=>", _setProductStartid)
	console.log("setProductStarttimeLimit=>", _setProductStarttimeLimit)
	contract.methods.setAuctionStart(_setProductStartid, _setProductStarttimeLimit).send({
		from: accounts[0]
	}).then(
		function (receipt) {
			// console.log(receipt)==>true
			if (receipt == true) {
				alert("设置开始成功...您可以开始拍卖了^v^")
			}
		});
});


// //3.查询指定商品的状态
// $(".getProductStatus").click(function() {
//     //获取id
//     let _getProductStatusid = 0;
//     console.log("getProductStatusid=>", _getProductStatusid)
//     contract.methods.showProductstatus(_getProductStatusid).call().then(
//         function(receipt) {
//             // console.log("addresult=>", receipt)
//             console.log(receipt)
//             $("#ShowProductid").html(receipt[0].toString());
//             $("#ShowProductStatus").html(receipt[1].toString());
//         });
// });




//3.查询当前账户余额和地址
$(".search_current_balance").click(function () {
	contract.methods.getNewInformation().call({
		from: accounts[0]
	}).then(
		function (receipt) {
			console.log("receipt=>", receipt)
			alert("当前账户地址为  :" + "\n" + receipt[0] + "\n" + "\n" + "当前账户余额为  :" + "\n" + receipt[1].toString() + "  WEI")
		});
});



//8.查询当前交易池金额
$(".search_currentcontract_balance").click(function () {
	contract.methods.queryBalance().call().then(
		function (receipt) {
			console.log("receipt=>", receipt)
			confirm("当前交易池金额 : " + receipt + "  WEI")
		});
});


// 主页显示拍卖的商品信息
$(".getproductinfo").click(function () {
	let id = 0;
	console.log("getproductinfoid=>", id)
	// contract.methods.getMoney(id).call().then(
	//     function(receipt) {
	//         console.log("receipt22=>", receipt);
	//         $("#height_bidvalue").html(receipt);
	//     });
	contract.methods.getProduct(id).call().then(
		function (receipt) {
			console.log("receipt=>", receipt)
			$("#getproductinfoitemid").html(receipt[0]);
			$("#getproductinfoitemname").html(receipt[1]);
			$("#getproductinfoitemcategory").html(receipt[2]);
			$("#getproductinfoitemstartPrice").html(receipt[3]);
			$("#getproductinfoitemower").html(receipt[4]);
			console.log(receipt[4])
			$("#getproductinfoitemstatus").html(receipt[5].toString());
		});
});